package com.xiaoke.api.controller;

import com.xiaoke.api.service.SearchHistoryService;
import com.xiaoke.entity.User;
import com.xiaoke.shiro.SecurityService;
import com.xiaoke.util.common.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/search")
public class SearchHistoryController {
    @Autowired
    private SearchHistoryService searchHistoryService;
    @Autowired
    private SecurityService securityService;

    @GetMapping("/clearhistory")
    public AjaxMsg clearHistory(String shopId){
        User user = securityService.getCurrentUser();
        searchHistoryService.deleteByUser(user.getId(),shopId);
        return new AjaxMsg();
    }

    @GetMapping("/historyKeywordList")
    public AjaxMsg historyKeywordList(String shopId){
        User user = securityService.getCurrentUser();
        List<String> keywords = searchHistoryService.findTop10ByUserAndShop(user.getId(),shopId);
        return new AjaxMsg(keywords);
    }
}
